home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr35 / qname3.zip / QNAME3.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  5KB  |  158 lines

  1. /*  QNAME3.C
  2. =========================================================================
  3.  
  4.     Original program -- QNAME.C
  5.  
  6.     QNAME.C -- renames ATBBS.QWK in current directory to new file name
  7.     that contains current day and month.
  8.     Written by Martin Leon, Ashton Tate Software Support using the
  9.     Microsoft Quick C compiler.
  10.  
  11. =========================================================================
  12.  
  13.     QNAME.C modified to QNAME2.C
  14.     by David J. Semon
  15.  
  16.     March 22, 1991
  17.  
  18.     Program modified to:
  19.  
  20.     1. Compile with Borland Turbo C 2.0 (struct date d) vs Microsoft
  21.        Quick C (dosdate_t today).
  22.     2. Allow any file name to be entered as a command line argument.
  23.     3. Rename the input file to a new file name that contains the first
  24.        three characters of the input file name plus current day, month
  25.        and a sequence letter (A,B,C,D, etc.).
  26.  
  27.        Examples:     Assume DOS date = 03/22/91
  28.                      Assume first file renamed on that date
  29.  
  30.                      ATBBS    becomes  ATB0322A.QWK
  31.                      SEMWARE  becomes  SEM0322A.QWK
  32.                      ABCDEFGH becomes  ABC0322A.QWK
  33.                      AB       becomes  AB0322A.QWK
  34.                      A        becomes  A0322A.QWK
  35.  
  36.     4. If no file name is provided, a message displays correct syntax.
  37.     5. If the file name is not found, a message is displayed saying so.
  38.  
  39. =======================================================================
  40.  
  41.    QNAME2.C  Modified to QNAME3.C  By Henry Gerlach on 5-12-93
  42.  
  43.    1.  remodified to again compile with MS Quick C.
  44.  
  45.    2.  Program now accepts additional arguments; Additional paths
  46.        in which to look for a file matching the prospective new name.
  47.  
  48.    3.  File to be changed need not be in current directory. A path
  49.        may be included with the name.
  50.  
  51.    4.  extension need not be .QWK. if no extension is given, .QWK
  52.        will be assumed.
  53. =======================================================================*/
  54.  
  55. #include <stdio.h>
  56. #include <io.h>
  57. #include <dos.h>
  58. #include <string.h>
  59. #include <process.h>
  60. FILE *oldfile;
  61. FILE *newfile;
  62. struct dosdate_t today;
  63. char newname[80];
  64. char oldname[80];
  65. char path_newname[99];
  66. char drive[3],dir[40],name[9],ext[4];
  67. char prefix[80],tprefix[4];
  68. int x,exists=0,numpath=2;
  69.  
  70. /*-------------------------------------------------------------------- */
  71. int main(int argc, char *argv[])
  72.   {
  73.   if(argc < 2)                        /* No argument entered */
  74.     {
  75.     printf("Please enter a file name to rename ...\n");
  76.     printf("Syntax: QNAME3 <filename>\n\n");
  77.     exit(1);
  78.     }
  79.   else
  80.     {
  81.     _splitpath(argv[1],drive,dir,name,ext);  /* break name into components */
  82.     if (!strlen(ext)) strcpy (ext,"QWK\0"); /* If no extension was given,
  83.                                              Assume .QWK      */
  84.     if(strlen(name) > 3)         /* If name of file is greater than */
  85.       {                             /*         three chars,            */
  86.       strncat(tprefix,name,3);  /* Copy first three characters */
  87.       }
  88.     else                            /*            else             */
  89.       {
  90.       strcpy(tprefix,name);     /* Copy entire string */
  91.       }
  92.     _makepath (oldname,drive,dir,name,ext);   /* add path back to name */
  93.     _makepath (prefix,drive,dir,tprefix,"\0");  /* add path back to prefix */
  94.     strupr(oldname);                /* Convert name to upper case */
  95.     strupr(prefix);                 /* Convert prefix to upper case */
  96.     }
  97.  
  98.   /* Make sure file (oldname) exists */
  99.   if((oldfile = fopen(oldname,"rt")) == NULL)
  100.     {
  101.     printf("%s not found in current directory ...\n\n",oldname);
  102.     return 1;
  103.     }
  104.   else
  105.     /* The file exists and is open. Close it */
  106.    fclose(oldfile);
  107.  
  108.   /* Get today's date */
  109.     _dos_getdate( &today );
  110.  
  111.   /* x = letter of the alphabet.  Start at "A", ASCII 65 */
  112.   x = 65;
  113.  
  114.   /*  Use sprintf() to combine the month and day and the letter into
  115.       a file name.  Check to see if it doesn't exist and rename. Do
  116.       this until letter = "Z" */
  117.   do
  118.     {
  119.     sprintf(newname, "%s%02u%02u%c.QWK", prefix, today.month, today.day, x);
  120.  
  121.     if((newfile = fopen(newname,"rt")) != NULL) exists = 1; /* check current path */
  122.     while (numpath < argc){  /* check altenate paths */
  123.         sprintf (path_newname,"%s%s",argv[numpath],newname);
  124.         if((newfile = fopen(path_newname,"rt")) != NULL) exists = 1;
  125.         numpath++;
  126.     }
  127.     if (!exists)
  128.       {
  129.         /* File name doesn't exist, rename original to unused file name */
  130.       if(rename(oldname,newname) != 0)
  131.         {
  132.         printf("Error renaming %s to %s\n\n",oldname,newname);
  133.         return 1;
  134.         }
  135.       else
  136.           /* If the rename was succesful, we're done with this loop */
  137.        break;
  138.       }
  139.     else
  140.       /* If the file existed, close it and go to next file name */
  141.      fclose(newfile);
  142.      numpath = 2;
  143.      exists = 0;
  144.     }
  145.   while (++x <= 90);
  146.  
  147.   /* If more than 26 files exist for today's date, give error message */
  148.   if(x > 90)
  149.     {
  150.     printf("\nUnable to rename beyond %s%02u%02u%c.QWK\n",today.month, today.day, x - 1 );
  151.     return 1;
  152.     }
  153.  
  154.   /* Rename completed */
  155.   printf("%s renamed to %s\n\n",oldname,newname);
  156.   return 0;
  157.   }
  158.